package com.yanqu.road.server.cmd.activity.fivehero;

import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FiveHeroProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross3UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.*;

@Cmd(code = CrossProtocol.C3_CROSS_FIVE_HERO_GET_UNION_REQ, desc = "")
public class Cross3FiveHeroGetUnionReqCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        FiveHeroProto.CrossFiveHeroGetUnionReqMsg reqMsg = FiveHeroProto.CrossFiveHeroGetUnionReqMsg.parseFrom(packet.getBytes());
        int activityId = reqMsg.getActivityId();
        long gameServerId = reqMsg.getGameServerId();

        // 获取商会UID
        List<String> unionUidList = reqMsg.getUnionUidList();
        if (unionUidList == null || unionUidList.size() == 0) {
            return;
        }

        // 返回值
        FiveHeroProto.CrossFiveHeroGetUnionRespMsg.Builder builder = FiveHeroProto.CrossFiveHeroGetUnionRespMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setGameServerId(gameServerId);
        builder.setGroupId(reqMsg.getGroupId());
        for (String s : unionUidList) {
            CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(s);
            Map<Long, CrossUnionMember> unionMemberMap = CrossUnionMgr.getUnionMemberMap(s);
            if (unionInfo != null && unionMemberMap != null) {
                FiveHeroProto.FiveHeroUnionMsg.Builder tmp = FiveHeroProto.FiveHeroUnionMsg.newBuilder();
                tmp.setUnionUid(s);
                tmp.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionInfo)));
                for (CrossUnionMember member : unionMemberMap.values()) {
                    tmp.addMember(FiveHeroProto.FiveHeroUnionMemberInfoMsg.newBuilder()
                            .setUserId(member.getUserId())
                            .setServerId(member.getServerId())
                            .setPosition(member.getPosition())
                    );
                }
                builder.addUnion(tmp);
            }
        }
        UnionActivityGroup group = Cross3UnionActivityGroupMgr.getGroupData(reqMsg.getActivityId(),reqMsg.getGroupId());
        // 返回
//        MessageHelper.sendChannelPacket(gameServerId, 0, YanQuMessageUtils.buildMessage(CrossProtocol.C2_CROSS_FIVE_HERO_GET_UNION_RESP, builder));
        MessageHelper.sendUnionActivityGroupPacket(gameServerId, 0, CrossProtocol.C2_CROSS_FIVE_HERO_GET_UNION_RESP, builder,activityId,
                group.getCrossServerId(), reqMsg.getGroupId());
    }

    @Override
    public void sendData() {

    }

}
